package com.ruoyi.web.controller.ems;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeShipment;
import com.ruoyi.system.domain.material.MaterialGrouping;
import com.ruoyi.system.service.IEmsBarcodeShipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/4/10 13:07
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "华为成品出库表api")
@RequestMapping("/ems/barcodeShipment")
public class EmsBarcodeShipmentController extends BaseController {

    @Autowired
    IEmsBarcodeShipmentService iEmsBarcodeShipmentService;


    /**
     * 查询华为成品出库表
     */
    @ApiOperation(value = "华为成品出库表列表")
    @GetMapping("/list")
    public TableDataInfo list(EmsBarcodeShipment emsBarcodeShipment) {
        startPage();
        LambdaQueryWrapper<EmsBarcodeShipment> lqw = new LambdaQueryWrapper<EmsBarcodeShipment>();
        if (StringUtils.isNotBlank(emsBarcodeShipment.getPo())) {
            lqw.like(EmsBarcodeShipment::getPo, emsBarcodeShipment.getPo());
        }
        if (StringUtils.isNotBlank(emsBarcodeShipment.getItemCode())) {
            lqw.like(EmsBarcodeShipment::getItemCode, emsBarcodeShipment.getItemCode());
        }
        if (StringUtils.isNotBlank(emsBarcodeShipment.getBarCode())) {
            lqw.like(EmsBarcodeShipment::getBarCode, emsBarcodeShipment.getBarCode());
        }
        if (null != emsBarcodeShipment.getBeginTime()) {
            lqw.apply("created_date>='" + DateTime.of(emsBarcodeShipment.getBeginTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (null != emsBarcodeShipment.getEndTime()) {
            lqw.apply("created_date<='" + DateTime.of(emsBarcodeShipment.getEndTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (StringUtils.isNotBlank(emsBarcodeShipment.getSegment18())) {
            lqw.like(EmsBarcodeShipment::getSegment18, emsBarcodeShipment.getSegment18());
        }

        if (emsBarcodeShipment.getUploadFlag() != null) {
            lqw.like(EmsBarcodeShipment::getUploadFlag, emsBarcodeShipment.getUploadFlag());
        }
        List<EmsBarcodeShipment> list = iEmsBarcodeShipmentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 新增华为成品出库表列表
     */
    @Log(title = "新增华为成品出库表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增华为成品出库表")
    @PostMapping
    public AjaxResult add(@RequestBody EmsBarcodeShipment emsBarcodeShipment) {
        emsBarcodeShipment.setCreatedBy(SecurityUtils.getUsername());
        emsBarcodeShipment.setCreatedDate(new Date());
        return toAjax(iEmsBarcodeShipmentService.save(emsBarcodeShipment) ? 1 : 0);
    }

    /**
     * 修改华为成品出库表列表
     */
    @Log(title = "修改华为成品出库表", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改华为成品出库表")
    @PutMapping
    public AjaxResult edit(@RequestBody EmsBarcodeShipment emsBarcodeShipment) {
        return toAjax(iEmsBarcodeShipmentService.updateById(emsBarcodeShipment) ? 1 : 0);
    }

    /**
     * 删除物料分组
     */
    @Log(title = "删除华为成品出库表", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除华为成品出库表")
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iEmsBarcodeShipmentService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }
}
